package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.entity.Score;
import cn.edu.nit.sys.service.ScoreService;
import cn.edu.nit.sys.utils.R;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 罗文斌
 * @Date 2023/5/10
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @GetMapping("/utils/{id}")
    public R getGradeList(@PathVariable("id") String id) {
        return scoreService.getGradeList(id);
    }

    @GetMapping
    public R getList(Page<Score> page, String siftId, String siftSubject, String siftTime) {
        return scoreService.getList(page, siftId, siftSubject, siftTime);
    }

    @GetMapping("/student")
    public R getStudentList(String subjectId, String name) {
        return scoreService.getStudentList(subjectId, name);
    }

    @PostMapping
    public R addScore(@RequestBody Score score) {
        return scoreService.save(score) ? R.ok("录入成功") : R.error("录入失败");
    }

    @PutMapping
    public R updateScore(@RequestBody Score score) {
        UpdateWrapper<Score> wrapper = new UpdateWrapper<>();
        wrapper.set("score", score.getScore());
        wrapper.eq("id", score.getId());
        return scoreService.update(wrapper) ? R.ok("修改成功") : R.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public R removeScore(@PathVariable("id") String id) {
        return scoreService.removeById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @GetMapping("/user/{id}")
    public R getUserScore(@PathVariable("id") String id) {
        return scoreService.getUserScore(id);
    }
}
